package carassius.DAL;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;

/**
 *
 * @author siebz0r
 */
public abstract class Table<T extends Row<K>, K>
{
	private final HashMap<K, T> _rows;

	public Table()
	{
		_rows = new HashMap<K, T>();
	}

	final HashMap<K, T> getRows()
	{
		return _rows;
	}

	/**
	 * Wrapper for <code>getRows().put()</code> as the key <K> is known by <T>
	 * only <T> is needed.
	 * @param row The row as presented by <T>
	 * @see Row
	 */
	final void put(T row)
	{
		synchronized (_rows)
		{
			_rows.put(row.getPrimaryKeys(), row);
		}
	}

	public abstract Collection<T> getAll() throws SQLException;

	public abstract K add(Object[] args) throws SQLException;

	public final boolean delete(T row) throws SQLException
	{
		return (row != null && delete(row.getPrimaryKeys()));
	}

	public abstract boolean delete(K keys) throws SQLException;

	public abstract T getByPrimaryKey(K key) throws SQLException;
}
